// 诉状
Vue.component('ht-plaint', {
    name:'ht-plaint',
    template: `
        <el-dialog title="" :visible.sync="show" v-cloak
            :destroy-on-close="true" width="90%" top="20px" @close="close"
            :append-to-body="true" z-index="9999999">
            <el-form label-width="120px" :data="itemData" size="mini">
                <el-form-item label="诉状">
                    <el-input type="text" placeholder="诉状" v-model="path" style="width: 70%"></el-input>
                    <el-upload :on-success="onSuccess" style="display: inline-block"
                        action="/admin/lawyer.project_files/upload"
                        :show-file-list="false" :limit="1">
                        <el-button size="mini" type="primary">点击上传</el-button>
                    </el-upload>
                    <el-button size="mini" type="success" @click="viewPlaint" v-if="can_edit">编辑/预览</el-button>
                    <div class="ht-text2" style="display: block">可以使用模板，也可以直接上传文件</div>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="itemData.suzhuang_status" >
                        <el-option :value="-1" label="待处理"></el-option>
                        <el-option :value="1" label="已寄出"></el-option>
                        <el-option :value="2" label="已返回"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="itemData.suzhuang_bz" placeholder="备注"></el-input>
                </el-form-item>
                <el-form-item label="">
                    <el-button type="primary" @click="doAdd">确定</el-button>
                    <el-button type="" @click="close">返回</el-button>
                </el-form-item>
            </el-form>
            <div class="ht-title">模板</div>
            <el-table :data="tableData" border header-row-class-name="ht-table-header"
                      ref="dataTable" @selection-change="handleSelectChange" row-key="title">
                <el-table-column align="center" type="selection" width="39"></el-table-column>
                <el-table-column prop="name" label="类型" width="100px" align="center">
                    <template slot-scope="scope">
                        <ht-table-image :value="scope.row"></ht-table-image>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="type_name" label="模板类型"></el-table-column>
                <el-table-column align="center" prop="title" label="名称"></el-table-column>
                <el-table-column align="center" prop="project.title" label="所属项目"></el-table-column>
                <el-table-column align="center" prop="create_time" width="120" label="添加时间"></el-table-column>
                <el-table-column align="center" label="操作" width="180">
                    <template slot-scope="scope">
                        <el-link size="mini" type="primary" @click="useTemplate(scope.row)">使用模板</el-link>
                    </template>
                </el-table-column>
            </el-table>
            <div class="ht-main-page">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="page"
                        :page-sizes="[1,10, 200, 300, 400]"
                        :page-size="limit"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
        </el-dialog>
    `,
    props: {
        itemData:{
            type:Object,
            default:()=>{
                return {

                }
            }
        },
        show:{
            type:Boolean,
            default:false
        },
    },
    data() {
        return {
            proof:{},
            tableData:[],
            files:{},
            path:'',
            old_path:'',
            total: 0,
            page: 1,
            limit: 10,
            can_edit:false
        }
    },
    created() {
        this.getTemplate()
        this.getPlaint()
        console.log("this.itemData",this.itemData)
    },
    methods: {
        useTemplate(row) {
            let vs = this
            if (!this.itemData.id) return;
            vs.confirm("使用模板将覆盖现有数据，确定操作?",()=>{
                Api.request('/admin/lawyer.project_files/useTemplate',{
                    project_item_id:this.itemData.id,
                    project_id:this.itemData.project_id,
                    template_id:row.id,
                    type:'suzhuang'
                }).then(res=>{
                    vs.$emit('update:show',false)
                    vm.getData()
                    window.open("/wps/index/index?file_id=" + res.data.file_id + "&type=getPlaint&item_id=" + vs.itemData.id)
                })
            })
        },
        viewPlaint() {
            if (!this.files) {
                this.can_edit = false;
                return
            }
            window.open("/wps/index/index?file_id="  + this.files.file_id + "&type=getPlaint&item_id=" + this.itemData.id)
        },
        getTemplate() {
            let vs = this
            if (!this.itemData.id) return;
            Api.request('/admin/lawyer.project_files/getTemplate',{
                project_id:this.itemData.project_id,
                project_type:'suzhuang'
            }).then(res=>{
                vs.tableData = res.data.data
                vs.total = res.data.total
            })
        },
        getPlaint(){
            let vs = this
            if (!this.itemData.id) return;
            Api.request('/admin/lawyer.project_files/getTemplateOfType',{
                project_item_id:this.itemData.id,
                project_type:'suzhuang'
            }).then(res=>{
                if (res.data) {
                    vs.files = res.data
                    vs.path = JSON.parse(JSON.stringify(vs.files)).path
                    vs.old_path = res.data.path
                    vs.can_edit = true
                } else {
                    vs.path = ''
                    vs.old_path = ''
                    vs.can_edit = false
                    vs.files = ''
                }
            })
        },
        delPlaint(){
            this.$confirm('确定删除?').then(()=>{
                Api.request('/admin/lawyer.project_files/delPlaint',{id:this.itemData.id}).then(res=>{
                    vm.showSuccessMsgCall(()=>{
                        this.path = ''
                        this.old_path = ''
                        vm.getData()
                    })
                })
            })
        },
        filesView() {
            let vs = this
            if (!vs.files) return;
            console.log(vs.files)
            window.open("/wps/index/index/file_id/" + vs.files.file_id)
        },
        onSuccess(res,file) {
            let vs = this
            console.log("vs.itemData",vs.itemData)
            if (res.code == 1) {
                vs.files = res.data
                vs.path = res.data.path
                console.log('upload',vs.files)
            } else {
                this.$message.error(res.msg)
            }
        },
        doAdd(){
            let vs = this
            let url = "/admin/lawyer.project_files/addPlaint"
            let postData = {
                id:vs.itemData.id,
                project_id:vs.itemData.project_id,
                suzhuang_status:vs.itemData.suzhuang_status,
                suzhuang_bz:vs.itemData.bz,
                files:vs.files
            }
            Api.request(url,postData).then(res=>{
                vm.showSuccessMsgCall(()=>{
                    vs.$emit('update:show',false)
                    vm.getData()
                })
            })
        },
        close(){
            this.$emit('update:show',false)
            console.log('conse')
        },
        handleSizeChange: function (e) {
            this.limit = e
        },
        handleCurrentChange: function (e) {
            this.page = e
        }
    },
    watch: {
        itemData(v) {
            this.getTemplate()
            this.getPlaint()
        }
    }
})